package com.health.trans.common.entity;

import com.health.trans.common.util.GenericUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * api反应
 *
 * @author nqlz
 * @date 2022/04/15
 */
@Data
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "接口返回参数格式")
public class ApiResponse<T> implements Serializable {

    private static final long serialVersionUID = -8810074710372408264L;

    @ApiModelProperty(value = "编码", example = "200", required = true)
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String msg;

    @ApiModelProperty(value = "返回数据<泛型>,依据业务实际情况确定具体类型")
    private T data;

    /**
     * 好吧
     * 请求成功
     *
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> ok() {
        return new ApiResponse<>(CodeEnum.SUCCESS.getCode(), "成功", null);
    }

    /**
     * 好吧
     * 请求成功
     *
     * @param successNum 成功数
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> ok(int successNum) {
        return GenericUtil.get(ok(new ErrorDataInfo(successNum, 0, null)));
    }

    public static <T> ApiResponse<T> ok(T data) {
        return new ApiResponse<>(CodeEnum.SUCCESS.getCode(), "成功", data);
    }

    public static <T> ApiResponse<T> error(String msg) {
        return new ApiResponse<>(CodeEnum.ERROR.getCode(), msg, null);
    }

    public static <T> ApiResponse<T> response(Integer code, String msg) {
        return new ApiResponse<>(code, msg, null);
    }

    public static <T> ApiResponse<T> response(Integer code, String msg,T object) {
        return new ApiResponse<>(code, msg, object);
    }

    /**
     * 令牌零
     * TOKEN错误
     *
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> tokenNull() {
        return response(CodeEnum.TOKEN_IS_NULL.getCode(), "token不能为空");
    }

    /**
     * 令牌错误
     * TOKEN错误
     *
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T>  tokenError() {
        return response(CodeEnum.TOKEN_ERROR.getCode(), "token信息错误");
    }

    /**
     * api联合国授权
     * 接口权限错误
     *
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> apiUnAuthorized() {
        return response(CodeEnum.API_UNAUTHORIZD.getCode(), "接口权限不足,请联系产商");
    }

    /**
     * 数据误差
     * 数据错误
     *
     * @param successNum 全国矿工工会成功
     * @param msg        味精
     * @param ex         错误
     * @param data       数据
     * @return {@link ApiResponse}<{@link ErrorDataInfo}>
     */
    public static ApiResponse<ErrorDataInfo> dataError(int successNum, String msg, Exception ex, List<?> data) {

        int errorNum = 0;

        if (data != null && !data.isEmpty()) {
            errorNum = data.size();
        }

        StringBuffer buffer = new StringBuffer();

        if (ex.getMessage() != null && !"".equals(ex.getMessage())) {
            final Writer result = new StringWriter();

            final PrintWriter printWriter = new PrintWriter(result);

            ex.fillInStackTrace().printStackTrace(printWriter);

            String content = result.toString();

            String regEx = "Caused by:(.*)";
            Pattern pat = Pattern.compile(regEx);
            Matcher mat = pat.matcher(content);

            while (mat.find()) {
                buffer.append(mat.group() + ";");
            }

        }

        return response(CodeEnum.DATA_ERROR.getCode(), msg + buffer.toString(), new ErrorDataInfo(successNum, errorNum, data));
    }

    /**
     * 数据误差
     * 数据错误
     *
     * @param successNum 全国矿工工会成功
     * @param msg        味精
     * @param data       数据
     * @return {@link ApiResponse}<{@link ErrorDataInfo}>
     */
    public static ApiResponse<ErrorDataInfo> dataError(int successNum, String msg, List<?> data) {

        int errorNum = 0;

        if (data != null && !data.isEmpty()) {
            errorNum = data.size();
        }

        ErrorDataInfo dataInfo = new ErrorDataInfo(successNum, errorNum, data);

        return response(CodeEnum.DATA_ERROR.getCode(), msg, dataInfo);
    }

    /**
     * 数据误差
     * 数据错误
     *
     * @param msg  味精
     * @param data 数据
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> dataError(String msg, T data) {
        return response(CodeEnum.DATA_ERROR.getCode(), msg, data);
    }

    /**
     * 系统错误
     * 系统错误错误
     *
     * @param msg 味精
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> systemError(String msg) {

        return response(CodeEnum.SYSTEM_ERROR.getCode(), msg, null);
    }


}
